/*
 * Crypto
 *
 * Creado el 03/01/2009, 11:47:52 AM
 */
package com.unicon.autoservicio.util;

/*
 * Crypto.java
 *
 * Created on 21 de febrero de 2006, 12:01 PM
 */
/**
 *
 * @author Ronald Ramirez
 */
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;

public class Crypto {

   private static byte[] SALT_BYTES = {
      (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32,
      (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03
   };
   private static int ITERATION_COUNT = 19;

   public static String encrypt(String str, String passPhrase) {
      Cipher ecipher = null;
      Cipher dcipher = null;

      try {
         // Crear la key

         KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(),
                 SALT_BYTES, ITERATION_COUNT);
         SecretKey key = SecretKeyFactory.getInstance(
                 "PBEWithMD5AndDES").generateSecret(keySpec);
         ecipher = Cipher.getInstance(key.getAlgorithm());
         dcipher = Cipher.getInstance(key.getAlgorithm());

         // Preparar los parametros para los ciphers

         AlgorithmParameterSpec paramSpec = new PBEParameterSpec(
                 SALT_BYTES, ITERATION_COUNT);

         // Crear los ciphers

         ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
         dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
      } catch (Exception e) {
         Logger logger = Logger.getLogger(Crypto.class.getName());
         logger.error(e);
      }

      try {
         // Encodear la cadena a bytes usando utf-8

         byte[] utf8 = str.getBytes("UTF8");

         // Encriptar

         byte[] enc = ecipher.doFinal(utf8);

         // Encodear bytes a base64 para obtener cadena
         Base64 b64 = new Base64();
         String value = new String(b64.encode(enc));
         return value;
      } catch (Exception e) {
         Logger logger = Logger.getLogger(Crypto.class.getName());
         logger.error(e);
         return null;
      }
   }



   public static String decrypt(String str, String passPhrase) {
      Cipher ecipher = null;
      Cipher dcipher = null;

      try {
         // Crear la key

         KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(),
                 SALT_BYTES, ITERATION_COUNT);
         SecretKey key = SecretKeyFactory.getInstance(
                 "PBEWithMD5AndDES").generateSecret(keySpec);
         ecipher = Cipher.getInstance(key.getAlgorithm());
         dcipher = Cipher.getInstance(key.getAlgorithm());

         // Preparar los parametros para los ciphers

         AlgorithmParameterSpec paramSpec = new PBEParameterSpec(
                 SALT_BYTES, ITERATION_COUNT);

         // Crear los ciphers

         ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
         dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
      } catch (Exception e) {
         Logger logger = Logger.getLogger(Crypto.class.getName());
         logger.error(e);
      }

      try {
         // Decodear base64 y obtener bytes
         Base64 b64 = new Base64();
         byte[] dec = b64.decode(str.getBytes());

         // Desencriptar

         byte[] utf8 = dcipher.doFinal(dec);

         // Decodear usando utf-8

         return new String(utf8, "UTF8");
      } catch (Exception e) {
         Logger logger = Logger.getLogger(Crypto.class.getName());
         logger.error(e);
         return null;
      }
   }
}


